package com.landian.springdata.demo.impl.service;

import com.landian.springdata.demo.api.enums.ResultEnum;
import com.landian.springdata.demo.api.exception.ProductException;
import com.landian.springdata.demo.api.service.ProductInfoService;
import com.landian.springdata.demo.api.vo.DecreaseStockInput;
import com.landian.springdata.demo.api.vo.ProductInfo;
import com.landian.springdata.demo.impl.converter.ProductInfoConverter;
import com.landian.springdata.demo.impl.entity.ProductInfoEntity;
import com.landian.springdata.demo.impl.entity.QProductInfoEntity;
import com.landian.springdata.demo.impl.predicable.ProductInfoPredicable;
import com.landian.springdata.demo.impl.repository.ProductInfoRepository;
import com.querydsl.core.types.Path;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 *
 * @author caojl
 **/
@Service
public class ProductInfoServiceImpl implements ProductInfoService {

    @Autowired
    private ProductInfoRepository productInfoRepository;

    private Path getIdPath(){
        return ProductInfoPredicable.idPath;
    }

    private QProductInfoEntity getQEntity(){
        return ProductInfoPredicable.QEntity;
    }

    @Override
    public ProductInfo findById(String id) {
        ProductInfoEntity entity = productInfoRepository.findOne(id);
        return ProductInfoConverter.vo.convert(entity);
    }

    @Override
    public List<ProductInfo> findById(List<String> ids) {
        List<ProductInfoEntity> list = productInfoRepository.findAll(ids);
        return ProductInfoConverter.vo.convert(list);
    }

    @Override
    public void decreaseStock(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfo> productInfoList = decreaseStockProcess(decreaseStockInputList);

        //发送mq消息
//        List<ProductInfoOutput> productInfoOutputList = productInfoList.stream().map(e -> {
//            ProductInfoOutput output = new ProductInfoOutput();
//            BeanUtils.copyProperties(e, output);
//            return output;
//        }).collect(Collectors.toList());
//        amqpTemplate.convertAndSend("productInfo", JsonUtil.toJson(productInfoOutputList));

    }

    @Transactional(rollbackOn = Exception.class)
    public List<ProductInfo> decreaseStockProcess(List<DecreaseStockInput> decreaseStockInputList) {
        List<ProductInfo> productInfoList = new ArrayList<>();
        for (DecreaseStockInput decreaseStockInput: decreaseStockInputList) {
            String productId = decreaseStockInput.getProductId();
            Optional<ProductInfo> productInfoOptional = Optional.ofNullable(findById(productId));
            //判断商品是否存在
            if (!productInfoOptional.isPresent()){
                throw new ProductException(ResultEnum.PRODUCT_NOT_EXIST);
            }
            ProductInfo productInfo = productInfoOptional.get();
            //库存是否足够
            Integer result = productInfo.getProductStock() - decreaseStockInput.getProductQuantity();
            if (result < 0) {
                throw new ProductException(ResultEnum.PRODUCT_STOCK_ERROR);
            }
            productInfoRepository.update(productId, getIdPath(), getQEntity().productStock, result);
            productInfoList.add(productInfo);
        }
        return productInfoList;
    }
}
